package com.leandro.CalendarioDeCompromissos;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class TelaRegistro extends Activity {

	Data data = new Data();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tela_registro);

		final Calendar calendario = Calendar.getInstance();

		data.ano = calendario.get(Calendar.YEAR);
		data.mes = calendario.get(Calendar.MONTH);
		data.dia = calendario.get(Calendar.DAY_OF_MONTH);
		data.hora = calendario.get(Calendar.HOUR_OF_DAY);
		data.minuto = calendario.get(Calendar.MINUTE);

		TextView txtData = (TextView) findViewById(R.id.txtRData);
		TextView txtHora = (TextView) findViewById(R.id.txtRHora);

		// Atribuir data atual ao textview
		txtData.setText(new StringBuilder().append(data.dia).append("/")
				.append(data.mes + 1).append("/").append(data.ano).append(" "));

		// Atribuir hora atual ao textview
		txtHora.setText(new StringBuilder().append(data.hora).append(":")
				.append(data.minuto > 9 ? data.minuto : "0" + data.minuto)
				.append(" "));

		View btnCancelar = (Button) findViewById(R.id.btnRCancelar);
		btnCancelar.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					// Intent i = new Intent(TelaRegistro.this,
					// TelaInicial.class);
					// startActivity(i);
					finish();
				} catch (Exception e) {
					Log.e("Erro", null);
				}
			}

		});

		View btnSalvar = (Button) findViewById(R.id.btnRSalvar);
		btnSalvar.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					finish();
				} catch (Exception e) {
					Log.e("Erro", null);
				}
			}

		});

		View defData = (Button) findViewById(R.id.RdefData);
		defData.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					DatePickerDialog dialog = new DatePickerDialog(
							TelaRegistro.this, datePickerListener, data.ano,
							data.mes, data.dia);
					dialog.show();
				} catch (Exception e) {
					Log.e("Erro", null);
				}
			}

		});

		View defHora = (Button) findViewById(R.id.RdefHora);
		defHora.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					TimePickerDialog dialog = new TimePickerDialog(
							TelaRegistro.this, timePickerListener, data.hora,
							data.minuto, true);
					dialog.show();
				} catch (Exception e) {
					Log.e("Erro", null);
				}
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tela_registro, menu);
		return true;
	}

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int anoSelecionado,
				int mesSelecionado, int diaSelecionado) {

			data.ano = anoSelecionado;
			data.mes = mesSelecionado;
			data.dia = diaSelecionado;

			TextView txtData = (TextView) findViewById(R.id.txtRData);

			txtData.setText(new StringBuilder().append(data.dia).append("/")
					.append(data.mes + 1).append("/").append(data.ano)
					.append(" "));

		}
	};

	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

		// when dialog box is closed, below method will be called.
		public void onTimeSet(TimePicker view, int horaSelecionada,
				int minutoSelecionado) {

			TextView txtHora = (TextView) findViewById(R.id.txtRHora);

			data.hora = horaSelecionada;
			data.minuto = minutoSelecionado;

			txtHora.setText(new StringBuilder().append(data.hora).append(":")
					.append(data.minuto > 9 ? data.minuto : "0" + data.minuto)
					.append(" "));

		}
	};

}
